package cn.arifun.handler;

import cn.arifun.domain.R;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import java.io.IOException;
import java.util.Collection;

/**
 * @Author: 漆原
 * @Description: 自定义成功校验处理器
 * @DateTime: 2024/10/2 11:57
 **/
public class JsonAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
        Object principal = authentication.getPrincipal();//用户身份
        Object credentials = authentication.getCredentials();//用户密码
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();//权限列表

        R result = new R(1, "登录成功", principal);

        response.setContentType("application/json;charset=utf-8");
        response.getWriter().println(JSON.toJSONString(result));
    }
}
